﻿/* Project: Side Scrolling Shooter
 * Health power-up.
 * Last Update: 4/12/11,11:16PM
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TheFifthSpace
{
    public class PowerupHealth: DrawableGameComponent
    {
        public float posX, posY;
        public Texture2D sprite;
        //private SpriteBatch spriteBatch;
        private Vector2 offset;
        private float speed, dt;
        public float HP_value;

        public Vector2 Position
        {
            get
            {
                return new Vector2(posX, posY);
            }
            set
            {
                posX = value.X;
                posY = value.Y;
            }
        }
        
        public PowerupHealth(Game g, Vector2 p)
            : base(g)
        {
            Game.Components.Add(this);
            Position = p;
            Random r = new Random();
            speed = 20 + r.Next(90);
            if (posY > 100) speed *= -1;
            /* if Powerup is coming from the bottom of the screen,
            it should move upward */
        }

        // LOAD CONTENT
        protected override void LoadContent()
        {
            sprite = MainGame.powerupSprite;
            //spriteBatch = MainGame.spriteBatch;
            offset = new Vector2(sprite.Width / 2, sprite.Height / 2);
        }

        public void Die()
        {
            MainGame.Powerups1.Remove(this);
            Game.Components.Remove(this);
        }

        // DRAW
        public override void Draw(GameTime gameTime)
        {
            MainGame.spriteBatch.Begin();
            MainGame.spriteBatch.Draw(sprite, Position - offset, Color.White);
            MainGame.spriteBatch.End();
        }

        // Check to see if player is touching power-up
        public bool CollideWithPlayer(Rectangle pRect)
        {
            Rectangle myRect = new Rectangle(
                (int)(posX - offset.X), (int)(posY - offset.Y),
                sprite.Width, sprite.Height);
            if (myRect.Intersects(pRect)) return true;
            else return false;
        }

        // UPDATE
        public override void Update(GameTime gameTime)
        {
            // Change in time
            dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            posY += speed * dt; // Move accordingly
        }
    }
}
